<template>
  <view class="container">
    <form @submit="submitForm">
      <view class="form-item">
        <text>学号：</text>
        <input type="text" placeholder="请输入学号" v-model="studentId"  @input="onInput" required />
      </view>
      <view class="form-item">
        <text>真实姓名：</text>
        <input type="text" placeholder="请输入真实姓名" v-model="realName" required />
      </view>
      <view class="form-item">
        <text>邮箱：</text>
        <input type="email" placeholder="请输入邮箱" v-model="email" required />
      </view>
      <view class="form-item">
        <text>身份证号：</text>
        <input type="text" placeholder="请输入身份证" v-model="card" required />
      </view>
      <view class="form-item">
        <text>电话：</text>
        <input type="tel" placeholder="请输入电话" v-model="phone" required />
      </view>
      <view class="form-item">
        <text>学院：</text>
        <input type="text" placeholder="请输入学院全称" v-model="college" required />
      </view>
      <view class="form-item">
        <text>专业：</text>
        <input type="text" placeholder="请输入专业全称" v-model="major" required />
      </view>
      <view class="form-item">
        <text>班级：</text>
        <input type="text" placeholder="请输入班级" v-model="className" required />
      </view>
      <view class="form-item">
        <text>性别：</text>
       <radio-group @change="radioChange">
            <label>
              <radio value="1" :checked="gender === '1'"/>男
            </label>
            <label>
              <radio value="0" :checked="gender === '0'"/>女
            </label>
          </radio-group>
      </view>
      <button form-type="submit" class="submit-btn">修改</button>
    </form>
  </view>
</template>

<script>
export default {
  data() {
    return {
      studentId: '',
      realName: '',
      email: '',
      phone: '',
      college:'',
      major:'',
      className: '',
      card:'',
      gender: '1',
      debounceTimer: null,
      debounceDelay: 500// 延迟时间，单位是毫秒
    };
  },

  methods: {
    LoginOrNot(){
      if (!uni.getStorageSync("hasLogin")) {
      	this.$u.toast('请先登录哦');
        setTimeout(() =>{
          uni.redirectTo({
              url: '/pages/login/weixin'
          });
        },500)  
      }
    },
    radioChange(e) {
         this.gender = e.detail.value;
       },
     onInput(e) {
        clearTimeout(this.debounceTimer); // 清除之前的计时器
        this.debounceTimer = setTimeout(() => {
        this.getStudentInfo(e.target.value); // 延迟调用API
      }, this.debounceDelay);
       },

      getStudentInfo(studentId) {
          // 发起请求到后端
          this.$H.get(`user/studentInfo/${studentId}`).then(response => {
            // 假设后端返回的数据格式为 { name: "张三", class: "一班", ... }
            console.log(response);
            if (response.result) {
              this.realName = response.result.userName;
              this.phone = response.result.phone;
              this.college = response.result.college;
              this.className = response.result.className;
              this.gender = (response.result.sex).toString();
              this.email = response.result.email;
              this.card = response.result.card;
              this.major = response.result.major;
              // ...其他字段
            }
          }).catch(error => {
            // 处理错误
            console.error(error);
          });
        },
    
    
    submitForm(e) {
      e.preventDefault();
      if (this.studentId && this.realName && this.email && this.phone && this.className && this.card ) {
//if(this.gender==='男') sex
        const data = {
          sid: this.studentId,
          userName: this.realName,
          email: this.email,
          phone: this.phone,
          college: this.college,
          className: this.className,
          sex: this.gender,
          major:this.major,
          card: this.card,
        };
        this.enterCompetition(data);
      } else {
        console.log(this.studentId);
        console.log(this.realName);
        console.log(this.email);
        console.log(this.phone);
        console.log(this.className);
        console.log(this.gender);
        uni.showToast({
          title: '请填写完整信息',
          icon: 'none'
        });
      }
    },
    
    enterCompetition(data) {
      console.log(data);
      uni.request({
        // url: 'https://api.yhjoj.cn/app/user/studentupdata',
        url: 'https://icpc.zut.edu.cn/app/user/studentupdata',
        method: 'POST',
        data,
        success: (res) => {
          if (res.statusCode === 200 && res.data.code === 0) {
            uni.showToast({
              title: '修改成功！',
              icon: 'success'
            });
           
          } else {
            uni.showToast({
              title: '修改出错',
              icon: 'none'
            });
          }
          setTimeout(() => {
                  uni.navigateBack({
                      delta: 1 // 返回上一个页面
                  });
              }, 500); // 延迟500毫秒
          
        },
        fail: () => {
          uni.showToast({
            title: '网络错误，请检查网络连接',
            icon: 'none'
          });
        }
      });
    },
  },
  mounted() {
    this.LoginOrNot();
  }
};
</script>

<style>
.container {
  padding: 20px;
}
.form-item {
  margin-bottom: 10px;
}
.submit-btn {
  margin-top: 20px;
  background-color: #007aff;
  color: white;
}
</style>